/*
 * @Author: wangming
 * @Date: 2020-09-02 11:29:04
 * @LastEditors: wangming
 * @LastEditTime: 2022-04-11 09:31:47
 * @Description: file content
 */
const MomentLocalesPlugin = require("moment-locales-webpack-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");
const packageJson = require("./package.json");

var plugins = [
  new MonacoWebpackPlugin({
    languages: [
      "javascript",
      "css",
      "html",
      "json",
      "typescript",
      "scss",
      "less",
      "java",
      "sql",
    ],
  }),
  // new LodashModuleReplacementPlugin(),
  new MomentLocalesPlugin({
    localesToKeep: ["zh-cn"],
  }),
];
if (process.env.NODE_ENV === "production") {
  plugins.push(
    new CompressionPlugin({
      filename: "[path].gz[query]",
      algorithm: "gzip",
      test: /\.js$|\.css$/,
      threshold: 10240,
      minRatio: 0.8,
      deleteOriginalAssets: false,
    })
  );
}
const publicPath = process.env.VUE_APP_ROOT_URL + '/' + packageJson.version
const outputDir = "dist" + '/' + packageJson.version
module.exports = {
  // 部署应用包时的基本URL
  publicPath ,
  // 构建输出目录
  outputDir,
  // 静态资源(js、css、img、fonts)目录
  assetsDir: "static",
  // 是否开启eslint保存检测
  lintOnSave: false,
  // 是否使用包含运行时编译器的 Vue 构建版本
  runtimeCompiler: false,
  // 生产环境的 source map
  productionSourceMap: false,
  devServer: {
    port: process.env.VUE_APP_PORT,
    proxy: "https://hanma-paas-dev.hancode.com/",
  },
  chainWebpack: (config) => {
    config.plugin("html").tap((args) => {
      args[0].cdn = publicPath + process.env.CDN + "?v=1";
      return args;
    });
    config.optimization.splitChunks({
      chunks: "all",
      cacheGroups: {
        vendors: {
          name: "chunk-vendors",
          priority: 10,
          test: /[\\/]node_modules[\\/]/,
          chunks: "initial",
          reuseExistingChunk: true,
          enforce: true,
        },
        element: {
          name: "chunk-element",
          priority: 20,
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
          reuseExistingChunk: true,
        },
        monaco: {
          name: "chunk-monaco",
          priority: 30,
          test: /[\\/]node_modules[\\/]_?monaco-editor(.*)/,
          reuseExistingChunk: true,
        },
      },
    });
  },
  configureWebpack: {
    // 公用模块
    externals: {
      vue: "Vue",
      vuex: "Vuex",
      "vue-router": "VueRouter",
      axios: "axios",
      "@babel/standalone": "Babel",
    },
    plugins,
  },
  css: {
    loaderOptions: {
      sass: {
        sassOptions: {
          outputStyle: "expanded",
        },
      },
    },
  },
  pluginOptions: {
    autoRouting: {
      chunkNamePrefix: "page-",
    },
    i18n: {
      locale: "zh-CN",
      fallbackLocale: "zh-CN",
      localeDir: "locales",
      enableInSFC: false,
      enableBridge: false,
    },
  },
};
